package cn.xaut.剑指offer.m10;

/**
 * 剑指 Offer 15. 二进制中1的个数
 */
public class demo12 {

    public int hammingWeight(int n) {

        int res = 0;
        while (n != 0) {
            res += n & 1;
            n >>>= 1;
        }
        return res;
    }

    
    public int hammingWeight2(int n) {
        
        int res = 0;
        while (n != 0) {
            res ++;
            n &= n - 1;
        }
        return res;
    }
    
    
    public static void main(String[] args) {

        System.out.println(new demo12().hammingWeight(00000000000000000000000000001011));
        System.out.println(new demo12().hammingWeight2(00000000000000000000000000001011));
    }
}
